package com.prince.web.service.impl;

import com.prince.bean.CommonWebResponse;
import com.prince.bean.ResultParams;
import com.prince.bean.WebCode;
import com.prince.constant.Constants;
import com.prince.mapper.SpecificationsMapper;
import com.prince.model.Specifications;
import com.prince.model.SpecificationsExample;
import com.prince.page.Page;
import com.prince.util.DateUtil;
import com.prince.util.ListUtils;
import com.prince.util.MapUtil;
import com.prince.util.StringUtil;
import com.prince.web.service.SpecificationsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 规格
 * @Author: 王培喜 58528792@163.com
 * @Date: 2021/4/6 15:04
 */
@Service
public class SpecificationsServiceImpl implements SpecificationsService {

    @Resource
    private SpecificationsMapper specificationsMapper;
    /**
     * 功能描述: 分页查询规格
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @Override
    public Map<String, Object> findSpecificationsByPage(Map<String, Object> params, Page page) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        SpecificationsExample example = new SpecificationsExample();
        SpecificationsExample.Criteria criteria = example.createCriteria().andDelEqualTo(Constants.YES);
        //规格
        if (MapUtil.isNotEmpty(params, "name")) {
            criteria.andNameLike("%"+params.get("name")+"%");
        }
        //拼音码
        if (MapUtil.isNotEmpty(params, "pinyin")) {
            criteria.andPinyinLike(params.get("pinyin").toString());
        }
        //查询总数
        int total = specificationsMapper.countByExample(example);
        if (null != page) {
            example.setPage(page);
        }
        example.setOrderByClause("created desc");
        List<Specifications> list = specificationsMapper.selectByExample(example);
        return response.toHashMapByPage(total,list);
    }
    /**
     * 功能描述: 添加规格
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> addSpecifications(Specifications specifications) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        //先判断是否存在 不存在再添加
        if(StringUtil.isEmpty(specifications.getName())){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.parameterNull).toHashMap();
        }
        //校验名称是否存在
        boolean flag = checkTheName(specifications.getName());
        if(flag){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.dataDuplication).toHashMap();
        }

        //添加
        specifications.setUpdater(specifications.getCreater());
        specifications.setCreated(new Date());
        specifications.setDel(Constants.YES);
        int i = specificationsMapper.insertSelective(specifications);
        if(i < 1){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.dataDuplication).toHashMap();
        }
        return response.toHashMap();
    }
    /**
     * 功能描述: 校验名称
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:34
     */
    private boolean checkTheName(String name){
        if(StringUtil.isEmpty(name)){
            return true;
        }
        SpecificationsExample example = new SpecificationsExample();
        SpecificationsExample.Criteria criteria = example.createCriteria().andDelEqualTo(Constants.YES);
        criteria.andNameEqualTo(name.trim());
        List<Specifications> units = specificationsMapper.selectByExample(example);
        if(ListUtils.isNotEmpty(units)){
            return true;
        }
        return false;
    }
    /**
     * 功能描述: 修改规格
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> updateSpecifications(Specifications specifications) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        if (StringUtil.isNotEmpty(specifications.getName()) && StringUtil.isNotEmpty(specifications.getRemark())
                && StringUtil.stringNotEquals(specifications.getName(),specifications.getRemark())) {
            //校验
            boolean flag = checkTheName(specifications.getName());
            if(flag){
                return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.dataDuplication).toHashMap();
            }
        }
        specifications.setRemark(null);
        int i = specificationsMapper.updateByPrimaryKeySelective(specifications);
        if(i < 1){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.updateError).toHashMap();
        }
        return response.toHashMap();
    }
    /**
     * 功能描述: 删除规格
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> delSpecifications(Specifications specifications) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        specifications.setDel(Constants.NO);
        int i = specificationsMapper.updateByPrimaryKeySelective(specifications);
        if(i < 1){
            return CommonWebResponse.getCommonRespCodeMsgParams(WebCode.delError).toHashMap();
        }
        return response.toHashMap();
    }
    /**
     * 功能描述: 通过id查询
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> findSpecificationsById(Integer id) {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        Specifications specifications = specificationsMapper.selectByPrimaryKey(id);
        response.put("data",specifications);
        return response.toHashMap();
    }
    /**
     * 功能描述: 查询所有规格
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @Override
    public Map<String, Object> findSpecificationsAll() {
        ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
        SpecificationsExample example = new SpecificationsExample();
        example.createCriteria().andDelEqualTo(Constants.YES);
        List<Specifications> list = specificationsMapper.selectByExample(example);
        list.forEach(x->{
            x.setRemark(x.getName());
        });
        response.put("data",list);
        return response.toHashMap();
    }
}
